<?php

namespace app\api\controller\shop;

use app\common\controller\Api;
use think\Db;
use think\Exception;



/**
 * C端小程序接口
 */
class Res extends Api
{
    protected $noNeedLogin = ['tarbarlist', 'shoplist'];
    protected $noNeedRight = '*';


    /**
     * 底部图标列表
     * 
     * 
     * 
     */
    public function tarbarlist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        $where['status'] = 'normal';
        // $where['type'] = 'goods';
        // $where['category'] = 1;
        $where['shop_id'] = 1;
        $this->auth->id;

        $list = Db::name('tarbar')
            ->where($where)
            //->field('*')
            ->order('weigh')
            ->paginate();

        $this->success('ok', $list);

    }


    /**
     * 门店列表
     * 
     * 
     *  @param string $keyword  店铺名称
     */
    public function shoplist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');

        if ($keyword) {
            $where['shopname'] = ['like', '%' . $keyword . '%'];
        }
        $where['status'] = 'normal';
        // $where['user_id'] = $this->auth->id;
        $where['type'] = 0;

        $list = Db::name('wanlshop_shop')
            ->where($where)
            ->field('id,shopname,city,adds')
            ->order('updatetime desc')
            ->paginate();

        $this->success('ok', $list);

    }

    /**
     * 服务分类列表
     * 
     * 
     * 
     */
    public function category()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        $where['status'] = 'normal';
        $where['type'] = 'goods';
        $where['category'] = 1;
        // $where['user_id'] = $this->auth->id;
        $where['pid'] = 105;//服务子类

        $list = Db::name('wanlshop_category')
            ->where($where)
            ->field('id,name,weigh')
            ->order('weigh desc')
            ->paginate();

        $this->success('ok', $list);

    }

    /**
     *  服务列表
     * 
     * 
     *  @param string $keyword  商品名称
     *  @param int $shop_id     店铺ID
     *  @param string $category  商品名称
     */
    public function goods()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');
        $shop_id = $this->request->request('shop_id');
        $category = $this->request->request('category');


        if ($keyword) {
            $where['s.title'] = $keyword;
        }
        if ($category) {
            $where['s.category_id'] = $category;
        }
        $where['s.grounding'] = 1;
        $where['s.gdtype'] = '1';
        $where['s.shop_id'] = $shop_id;
        $where['s.activity_type'] = 'goods';
        $where['s.publishing_platform'] = '0';

        $list = Db::name('wanlshop_goods s')
            ->join('wanlshop_goods_sku c', 's.id = c.goods_id', 'left')
            ->where($where)
            ->field('c.id as suk_id,s.id as good_id,c.price,c.difference,s.title,s.image')
            ->order('c.id desc')
            ->paginate();

        $this->success('ok', $list);

    }


    /**
     * 获取解痛师(员工)
     */
    public function pain_reliever()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $shop_id = $this->request->request('shop_id');
        $where['s.shop_id'] = $shop_id;
        $where['s.status'] = 1;
        $list = Db::name('staff s')
                ->join('wanlshop_staff_rank sr','sr.id = s.rank_id','left')
                ->field('s.*, sr.name as staff_rank_name')
                ->where($where)->paginate();
        $this->success('ok', $list);
    }

    /**
     * 预约列表
     * 
     */
    public function reslist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);



        $where['r.shop_id'] = $this->auth->shop_id;
        $where['r.user_id'] = $this->auth->id;
        $where['r.status'] = 1;

        $list = Db::name('reservation r')
            ->where($where)
            ->field('r.*')
            ->order('r.id desc')
            ->paginate();

        $this->success('ok', $list);


    }




    /**
     * 新增预约
     * 
     * 
     *  @param int $stime  时间
     *  @param string $name  姓名
     *  @param string $mobile  手机号
     *  @param int $goods_id  商品id
     *  @param int $sku_id  skuid
     *  @param string $title  服务名称
     *  @param string $difference 规格
     *  @param int $quantity 数量
     *  @param string $remarks 规格
     */
    public function resadd()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $stime = $this->request->request('stime');
        $name = $this->request->request('name');
        $mobile = $this->request->request('mobile');
        $goods_id = $this->request->request('goods_id');
        $sku_id = $this->request->request('sku_id');
        $title = $this->request->request('title');
        $difference = $this->request->request('difference');
        $quantity = $this->request->request('quantity');
        $remarks = $this->request->request('remarks');

        $staff_id = $this->request->request('staff_id');


        $date = [
            'name' => $name,
            'stime' => $stime,
            'mobile' => $mobile,
            'goods_id' => $goods_id,
            'sku_id' => $sku_id,
            'title' => $title,
            'difference' => $difference,
            'quantity' => $quantity,
            'remarks' => $remarks,
            'shop_id' => $this->auth->shop_id,
            'user_id' => $this->auth->id,
            'staff_id' => $staff_id,
            'createtime' => time()

        ];

        $list = Db::name('reservation')->insert($date);

        $this->success('ok', $list);

    }

    /**
     * 大转盘抽奖
     * 
     * 
     *  @param int $draw_id  活动ID
     */
    public function draw()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $draw_id = $this->request->request('draw_id');


        $where['id'] = $draw_id;

        $list = Db::name('raffle')
            ->where($where)
            ->find();

        $time = time();
        if ($time < $list['starttime'] || $time > $list['endtime']) {
            $list['status'] = 'hidden';
        }

        $list['info'] = Db::name('raffle_awsrds')
            ->where('raffle_id', '=', $draw_id)
            // ->field('id,title,number,actual_payment,difference')
            ->select();

        $this->success('ok', $list);

    }


    /**
     * 抽奖结果
     * 
     * 
     *  @param int $draw_id  活动ID
     */
    public function drawadd()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $draw_id = $this->request->request('draw_id');

        $where['id'] = $draw_id;

        $list = Db::name('raffle')
            ->where($where)
            ->find();

        $time = time();

        if ($time < $list['starttime'] || $time > $list['endtime']) {

            $list = null;

        } else {

            $list = Db::name('raffle_awsrds')
                ->where('raffle_id', '=', $draw_id)
                // ->field('id,title,number,actual_payment,difference')
                ->select();

            $randomNumber = random_int(1, 100); 
            $cumulativeProbability = 0; 
            $winner = null; 

            foreach ($list as $prize => $probability) {
                $cumulativeProbability += $probability['winning'];

                if ($randomNumber <= $cumulativeProbability) { 

                    $winner = $prize;

                    break; 

                }
            }

            if ($winner) {
                $winner = $winner - 1;
                $raffle_awsrds_id = $list[$winner]['id'];
                $sum = Db::name('raffle_draw')
                    ->where('raffle_id', '=', $draw_id)
                    ->where('raffle_awsrds_id', '=', $raffle_awsrds_id)
                    ->count();

                if ($sum < $list[$winner]['quantity']) {
                    $win = true;
                }else {
                    $win = false;
                }
            } else {
                $win = false;
            }


            if ($win) {

                $date = [
                    'raffle_awsrds_id' => $raffle_awsrds_id,
                    'raffle_id' => $draw_id,
                    'user_id' => $this->auth->id,
                    'createtime' => time()

                ];

                $add = Db::name('raffle_draw')->insert($date);

                $list = $list[$winner];

            } else {

                $date = [
                    'raffle_awsrds_id' => 0,
                    'raffle_id' => $draw_id,
                    'user_id' => $this->auth->id,
                    'createtime' => time()

                ];
                $add = Db::name('raffle_draw')->insert($date);

                $lists = [
                    'id' => 0,
                    'name' => '谢谢参与'

                ];
                $list = $lists;


            }

        }

        $this->success('ok', $list);

    }

     /**
     * AI报告
     * 
     * 
     *  
     */
    public function aipdf()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
       
        $where['mobile'] = $this->auth->mobile;
        //var_dump($this->auth->mobile);
        // $where['user_id'] = $this->auth->id;

        $list = Db::name('ai')
            ->where($where)
           // ->field('id,shopname,city,adds')
           // ->order('updatetime desc')
            ->find();

        $this->success('ok', $list);

    }









}
